package lt.egidijus.aatest.wizards.pages;

	
	import java.util.ArrayList;
import java.util.List;

import lt.egidijus.aatest.filters.ATGOption;
import lt.egidijus.aatest.filters.InterfaceFilter;
import lt.egidijus.aatest.model.AAtestClass;
import lt.egidijus.aatest.model.AAtestList;
import lt.egidijus.aatest.wizards.helpers.ATGList;
import lt.egidijus.aatest.wizards.helpers.ATGTable;
import lt.egidijus.aatest.wizards.helpers.DefaultOptions;
import lt.egidijus.aatest.wizards.helpers.list.TestListTable;
import lt.egidijus.aatest.wizards.helpers.list.TestWizardList;

import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
	import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
	import org.eclipse.swt.layout.GridLayout;
	import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
	import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
	public class TestsListPage extends WizardPage {

		public List<AAtestClass> getSelectedTests() {
			//DefaultOptions.setDefaultInterfaces(table.getSelectedItems());
			List<AAtestClass> tmp = new ArrayList<AAtestClass>(0);
			for (AAtestClass o : table.getSelectedItems()) {
				tmp.add((AAtestClass) o);
			}
			return tmp;
		}

//		public InterfaceFilter getDefault() {
//			DefaultOptions.setDefaultInterface(sar.getSelectedItem());
//			return (InterfaceFilter) sar.getSelectedItem();
//		}

		protected TestWizardList sar = null;
		protected TestListTable table = null;
		protected AAtestList tList = null;

		/**
		 * Constructor for SampleNewWizardPage.
		 * 
		 * @param pageName
		 */
		public TestsListPage(ISelection selection, Shell shell, AAtestList list) {
			super("wizardPage");
			setTitle("Tests runner");
			//setDescription("Please select test.");
			this.tList = list;
		}

		public TestsListPage() {
			super("wizardPage");
			setTitle("Tests runner");
			//setDescription("Please select test.");
			tList = new AAtestList();
		}

		/**
		 * @see IDialogPage#createControl(Composite)
		 */
		public void createControl(Composite parent) {

			GridData gridData = new GridData();
			gridData.horizontalSpan = 2;
			gridData.horizontalAlignment = SWT.FILL;
			gridData.grabExcessHorizontalSpace = true;
			gridData.verticalAlignment = SWT.FILL;
			gridData.grabExcessVerticalSpace = true;
			Composite container = new Composite(parent, SWT.NULL);
			GridLayout layout = new GridLayout();
			container.setLayout(layout);
			layout.numColumns = 1;
			layout.verticalSpacing = 9;

			createElement(gridData, container, "&Select one or more tests:");
			dialogChanged();
			setControl(container);
		}

		private void createElement(GridData gridData, Composite container,
				String name) {
			Label label = new Label(container, SWT.NULL);
			label.setText(name);
			table = new TestListTable(this.getShell(), container, SWT.CHECK | SWT.BORDER
					| SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION,
					tList.getList());
			table.getTable().addListener(SWT.Selection, new Listener() {
				public void handleEvent(Event event) {
					if (table.getSelectedItems().size() == 0) {
						updateStatus("At least one test should be selected.");
					} else {
						updateStatus(null);
					}
				}
			});
//			sar = new TestWizardList(
//					this.getShell(),
//					container,
//					SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL
//							| SWT.FULL_SELECTION,
//							tList.getList(),
//					"Select default interface filter, which will be used for tested class invocation object generation:");
		}

		/**
		 * Ensures that both text fields are set.
		 */

		private void dialogChanged() {
			updateStatus(null);
		}

		private void updateStatus(String message) {
			setErrorMessage(message);
			setPageComplete(message == null);
		}
}
